package com.example.micrweb.front.controller;

import com.example.common.enums.RCode;
import com.example.common.util.CommonUtil;
import com.example.micrweb.front.view.PageInfo;
import com.example.micrweb.front.view.RespResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.api.model.ProductInfo;
import org.example.api.pojo.BidProductInfo;
import org.example.api.pojo.MultiProduct;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/12 下午 05:16
 * @version: v1.0
 */
@Tag(name = "产品服务")
@RestController
@RequestMapping("/v1")
public class ProductInfoController extends BaseController{

    @Operation(summary = "查询产品信息信息", description = "根据产品id查询产品详细信息、投资最新的五位信息")
    @GetMapping("/product/info")
    public RespResult queryProductDetail(@RequestParam("productId") Integer id){
        RespResult result = RespResult.fail();
        if (id != null && id > 0){
            // 调用产品查询，根据产品id查询产品信息
            ProductInfo productInfo = productInfoService.queryById(id);
            if (productInfo != null){
                // 查询投资记录，根据产品id查询产品的投资信息
                List<BidProductInfo> bidProductInfoList = investService.queryBidProductInfo(id, 1, 5);
                // 查询成功
                result.setRCode(RCode.SUCC);
                result.setData(productInfo);
                result.setList(bidProductInfoList);
            }else {
                result.setRCode(RCode.REQUEST_PRODUCT_OFF);
            }
        }
        return result;
    }


    @Operation(summary = "产品分页查询", description = "根据产品种类分页查询")
    @GetMapping("/product/list")
    public RespResult queryProductByType(@RequestParam("ptype") Integer productType,
                                         @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                         @RequestParam(value = "pageSize", required = false, defaultValue = "9") Integer pageSize){
        RespResult result = RespResult.fail();

        if (productType != null && productType == 1 || productType == 2 || productType == 0){
            pageNo = CommonUtil.defaultPageNO(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            Integer nums = productInfoService.queryRecordNumsByType(productType);
            if (nums > 0){
                List<ProductInfo> productInfos = productInfoService.queryByTypeLimit(productType, pageNo, pageSize);
                result = RespResult.success();
                result.setData(productInfos);
                // 构建pageInfo
                PageInfo page = new PageInfo(pageNo,pageSize,nums);
                result.setPage(page);
            }
        }else {
            result.setRCode(RCode.REQUEST_PRODUCT_TYPE_ERR);
        }
        return result;
    }

    @Operation(summary = "首页三类产品的列表", description = "一个新手宝，三个优选，三个散标")
    /*查询首页产品*/
    @GetMapping("/product/index")
    public RespResult queryProductIndex(){
        RespResult result = new RespResult();
        MultiProduct multiProduct = productInfoService.queryIndexPageProducts();
        result.setCode(1000);
        result.setMsg("产品信息查新成功");
        result.setData(multiProduct);
        return result;
    }

    @Operation(summary = "查询所有的产品")
    /*查询所有产品*/
    @GetMapping("/product/getall")
    public RespResult queryProductAll(){
        RespResult result = new RespResult();
        List<ProductInfo> productInfos = productInfoService.queryAllProduct();
        result.setCode(1000);
        result.setMsg("查询成功");
        result.setData(productInfos);
        return result;
    }
}
